import 'package:flutter/material.dart';

import '../utils/callbacks.dart';

class TagBoard extends StatefulWidget {
  final String tag;

  final bool alwaysShowButton;
  final VoidCallbackOneString? tagClick;
  final VoidCallbackOneString? onEdit;
  final VoidCallbackOneString? onDelete;
  final VoidCallbackOneString? onSelect;
  final VoidCallbackOneString? onHover;
  final bool showButtons;
  final bool isBlock;
  const TagBoard(
      {super.key,
      required this.tag,
      this.alwaysShowButton = false,
      this.onDelete,
      this.onEdit,
      this.onSelect,
      this.tagClick,
      this.onHover,
      this.isBlock = true,
      this.showButtons = false});

  @override
  State<StatefulWidget> createState() => TagBoardState();
}

class TagBoardState extends State<TagBoard> {
  bool _isHovering = false;
  WidgetSpan _buildInkWellInline(
      VoidCallbackOneString? onTap, IconData? iconData,
      {Color? color}) {
    return WidgetSpan(
        child: InkWell(
      onTap: () {
        onTap?.call(widget.tag);
      },
      child: Icon(
        iconData,
        size: 17,
        color: color,
      ),
    ));
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        widget.tagClick?.call(widget.tag);
      },
      onHover: widget.alwaysShowButton == true
          ? null
          : (value) {
              if (widget.onHover != null) {
                widget.onHover!.call(widget.tag);
              } else {
                setState(() {
                  _isHovering = value;
                });
              }
            },
      child: Container(
        padding: const EdgeInsets.fromLTRB(3, 1, 3, 1),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(3),
            color: widget.isBlock ? const Color(0xFFDDDDDD) : null),
        child: (widget.showButtons || _isHovering) || widget.alwaysShowButton
            ? Text.rich(TextSpan(text: widget.tag, children: [
                if (widget.onEdit != null)
                  _buildInkWellInline(widget.onEdit, Icons.edit),
                if (widget.onSelect != null)
                  _buildInkWellInline(
                      widget.onSelect, Icons.bookmark_added_rounded),
                if (widget.onDelete != null)
                  _buildInkWellInline(widget.onDelete, Icons.close,
                      color: Colors.red),
              ]))
            : Text(widget.tag),
      ),
    );
  }
}
